#include "ScnsAt24c02.c.h"

#if defined(SCNS_AT24C02_ENABLE)&&SCNS_AT24C02_ENABLE==1

#include "ScnsTimer.h"
#include "ScnsSmctp.h"

#define INTERNAL_BSS(x) __attribute__((section(".bss.__scnsAt24c02_" #x))) static

INTERNAL_BSS(rd) struct
{
    volatile uint8 inited;
}rd[SCNS_AT24C02_MAX];

#define INTERNAL_TEXT(x) __attribute__((section(".text.__scnsAt24c02_" #x))) static

INTERNAL_TEXT(WB) ScnsStatus WB(ScnsAt24c02Enum at2N,uint8 addr,uint8 data)
{
    return scnsBspAt24c02Write(at2N,&addr,1,&data,1);
}

INTERNAL_TEXT(RB) uint8 RB(ScnsAt24c02Enum at2N,uint8 addr)
{
    uint8 data=0;
    scnsBspAt24c02Read(at2N,&addr,1,&data,1);
    return data;
}

ScnsStatus scnsAt24c02Init(ScnsAt24c02Enum at2N)
{
    SCNS_ASSERT_ON_RUN(at2N<SCNS_AT24C02_MAX);
    scnsCheckStatus(scnsBspAt24c02Init(at2N));
    uint8 addr=0XFF,data=0;
    scnsCheckStatusTimeOut(200*1000,scnsBspAt24c02Read(at2N,&addr,1,&data,1));
    scnsCheckStatus(scnsBspAt24c02PostInit(at2N));
    rd[at2N].inited=1;
    return SCNS_STATUS_OK;
}

uint8 scnsAt24c02ReadByte(ScnsAt24c02Enum at2N,uint8 addr)
{
    SCNS_ASSERT_ON_RUN(at2N<SCNS_AT24C02_MAX);
    if(scnsUnlikely(!rd[at2N].inited))
    {
        return 0;
    }
    return RB(at2N,addr);
}

void scnsAt24c02WriteByte(ScnsAt24c02Enum at2N,uint8 addr,uint8 data)
{
    SCNS_ASSERT_ON_RUN(at2N<SCNS_AT24C02_MAX);
    if(scnsUnlikely(!rd[at2N].inited))
    {
        return;
    }
    WB(at2N,addr,data);
    scnsDelayUs(4000);
}

void scnsAt24c02ReadBytes(ScnsAt24c02Enum at2N,uint8 addr,uint8 data[],uint8 len)
{
    SCNS_ASSERT_ON_RUN(at2N<SCNS_AT24C02_MAX);
    if(scnsUnlikely(!rd[at2N].inited))
    {
        return;
    }
    for(uint8 i=0;i<len;++i)
    {
        data[i]=RB(at2N,addr+i);
    }
}

void scnsAt24c02WriteBytes(ScnsAt24c02Enum at2N,uint8 addr,uint8 data[],uint8 len)
{
    SCNS_ASSERT_ON_RUN(at2N<SCNS_AT24C02_MAX);
    if(scnsUnlikely(!rd[at2N].inited))
    {
        return;
    }
    for(uint8 i=0;i<len;++i)
    {
        WB(at2N,addr+i,data[i]);
        scnsDelayUs(4000);
    }
}

ScnsStatus scnsAt24c02Test(ScnsAt24c02Enum at2N,uint8 addr)
{
    SCNS_ASSERT_ON_RUN(at2N<SCNS_AT24C02_MAX);
    if(scnsUnlikely(!rd[at2N].inited)){return SCNS_STATUS_NOT_INITED;}
    uint8 data[16];
    uint8 start=scnsAt24c02ReadByte(at2N,addr)+1;
    for(uint8 i=0;i<16;data[i]=i+start,++i){}
    hxPrintfVFL(__file__,__LINE__,"%#04&16^8hhX\n",data);
    scnsAt24c02WriteBytes(at2N,addr,data,16);
    for(uint8 i=0;i<16;data[i]=0,++i){}
    scnsAt24c02ReadBytes(at2N,addr,data,16);
    hxPrintfVFL(__file__,__LINE__,"%#04&16^8hhX\n",data);
    for(uint8 i=0;i<16;++i)
    {
        if(data[i]!=i+start)
        {
            return SCNS_STATUS_FAILED;
        }
    }
    return SCNS_STATUS_OK;
}

#if defined(SCNS_WEAK)
//@formatter:off
SCNS_WEAK ScnsStatus scnsBspAt24c02Init(ScnsAt24c02Enum at2N){return SCNS_STATUS_OK;}
SCNS_WEAK ScnsStatus scnsBspAt24c02PostInit(ScnsAt24c02Enum at2N){return SCNS_STATUS_OK;}
//@formatter:on
#endif//SCNS_WEAK
#endif


//@scnsName:EEPROM
